package ca.eloas.judging.rest.resources;

import ca.eloas.judging.messages.JudgeListMessage;
import ca.eloas.judging.messages.JudgeMessage;
import ca.eloas.judging.rest.repositories.JudgeRepository;
import ca.eloas.judging.rest.transactions.Transactional;
import ca.eloas.restsupport.operations.LIST;
import ca.eloas.restsupport.utils.BasicResource;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import static ca.eloas.judging.rest.operations.ChildLink.childLink;
import static ca.eloas.judging.rest.operations.CopyToMessage.copyToMessage;
import static ca.eloas.judging.rest.operations.SelfLink.selfLink;
import static ca.eloas.restsupport.operations.AddLink.addLink;

/**
 * @author JP
 */
@Path("/judges")
public class JudgeResource extends BasicResource {

    @Inject
    private JudgeRepository judges;

    @Inject
    private ResourceFactory resourceFactory;

    @GET
    @Path("/")
    @LIST
    public JudgeListMessage listTeams() {

        return list(judges.listAll(), JudgeListMessage.class, JudgeMessage.class)
                .runOnList(
                        selfLink()
                ).runOnItems(
                        childLink("competitions", "/competitions"),
                        copyToMessage(),
                        selfLink()
                ).now();
    }

    @GET
    @Path("/{id}")
    public JudgeMessage getJudge(@PathParam("id") long id) {

        return get(judges.get(id), JudgeMessage.class)
                .runAll(
                        copyToMessage(),
                        childLink("competitions", "/competitions"),
                        selfLink()
                ).now();
    }

    @Path("/{id}/competitions")
    public JudgeCompetitionResource judgeCompetitionResource(@PathParam("id") long id) {

        return resourceFactory.createJudgeCompetitionResource(judges.get(id));
    }

}
